package tests;

import static org.junit.Assert.fail;
import static util.Constant.STR_LOGIN_ALREADY_EXISTS;
import static util.Constant.STR_USER_ALREADY_FRIEND;
import static util.Constant.STR_USER_NOT_CONNECTED;
import static util.Constant.STR_EXCEPTION_SHOULD_HAVE_OCCURED;

import lib.UserManager;
import base.User;

import org.junit.Test;
import junit.framework.Assert;

public class UserManagerTest {

	@Test
	public void testAreFriend() {
		UserManager userManager = UserManager.getInstance();
		userManager.initialize();
		String password = "123fc";
		User user1 = userManager.create("toto", password, "Brown", "Alphonse");
		User user2 = userManager.create("bob", "frx6", "Voon", "Bobby");

		user1.connect(password);
		try {
			Assert.assertFalse(userManager.areFriends(user1, user2));
			userManager.makeFriends(user1, user2);
			Assert.assertTrue(userManager.areFriends(user1, user2));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testConnect() {
		UserManager userManager = UserManager.getInstance();
		userManager.initialize();
		String login = "tata";
		String password = "134kio";
		userManager.create(login, password, "Brown", "Alphonse");
		Assert.assertFalse(userManager.connect(login + "f", password));
		Assert.assertFalse(userManager.connect(login, password + "f"));
		Assert.assertFalse(userManager.connect(login + "f", password + "f"));
		Assert.assertTrue(userManager.connect(login, password));
	}

	@Test
	public void testCreateUser() {
		UserManager userManager = UserManager.getInstance();
		userManager.initialize();
		userManager.create("toto", "frunkp", "Brown", "Alphonse");

		try {
			userManager.create("toto", "frunkp", "Brown", "Alphonse");
			fail(STR_EXCEPTION_SHOULD_HAVE_OCCURED);
		} catch (IllegalArgumentException e) {
			Assert.assertEquals(STR_LOGIN_ALREADY_EXISTS, e.getMessage());
		}
	}

	@Test
	public void testGet() {
		UserManager userManager = UserManager.getInstance();
		userManager.initialize();
		User user1 = userManager.create("toto", "frunkp", "Brown", "Alphonse");
		User user2 = null;

		try {
			user2 = userManager.get(user1.getID());
		} catch (Exception e) {
			e.printStackTrace();
		}
		Assert.assertEquals(user1.getID(), user2.getID());
	}

	@Test
	public void testMakeFriends() {
		UserManager userManager = UserManager.getInstance();
		userManager.initialize();
		String login = "toto";
		String password = "123fr9";
		User user1 = userManager.create(login, password, "Brown", "Alphonse");
		User user2 = userManager.create("toto2", "frunkp", "Brown", "Alphonse");
		try {
			userManager.makeFriends(user1, user2);
			fail(STR_EXCEPTION_SHOULD_HAVE_OCCURED);
		} catch (Exception e) {
			Assert.assertEquals(STR_USER_NOT_CONNECTED, e.getMessage());
		}
		
		user1.connect(password);
		try {
			userManager.makeFriends(user1, user2);
			userManager.makeFriends(user1, user2);
			fail(STR_EXCEPTION_SHOULD_HAVE_OCCURED);
		} catch (Exception e) {
			Assert.assertEquals(STR_USER_ALREADY_FRIEND, e.getMessage());
		}
	}

	@Test
	public void testRemoveFriends() {
		UserManager userManager = UserManager.getInstance();
		userManager.initialize();
		String login = "toto";
		String password = "123fr9";
		User user1 = userManager.create(login, password, "Brown", "Alphonse");
		User user2 = userManager.create("toto2", "frunkp", "Brown", "Alphonse");

		try {
			userManager.removeFriends(user1, user2);
		} catch (Exception e) {
			Assert.assertEquals(STR_USER_NOT_CONNECTED, e.getMessage());
		}

		user1.connect(password);
		try {
			userManager.makeFriends(user1, user2);
			userManager.makeFriends(user1, user2);
			fail(STR_EXCEPTION_SHOULD_HAVE_OCCURED);
		} catch (Exception e) {
			Assert.assertEquals(STR_USER_ALREADY_FRIEND, e.getMessage());
		}
	}
}
